package cn.edu.lsnu.mbti.controller;

import cn.edu.lsnu.mbti.common.R;
import cn.edu.lsnu.mbti.entity.Region;
import cn.edu.lsnu.mbti.exception.CustomerException;
import cn.edu.lsnu.mbti.service.RegionService;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("region")
@Slf4j
public class RegionController {

    @Resource
    private RegionService regionService;

    @DeleteMapping("delete/{id}")
    public R<String> deleteById(@PathVariable("id") String id){
        if (StrUtil.isBlank(id)){
            throw new CustomerException("无法删除空对象");
        }
        return R.success(regionService.deleteById(id));
    }

    @PostMapping("saveRegion")
    public R<String> saveRegion(@RequestBody Region region){
        if (ObjectUtil.isNull(region)){
            throw new CustomerException("不能保存空对象");
        }
        return R.success(regionService.saveRegion(region));
    }

    @GetMapping("queryPageRegionList")
    public R<Page<Region>> queryPageRegionList(@RequestParam("pageNum") int pageNum,
                                       @RequestParam("pageSize") int pageSize,
                                       @RequestParam(value = "name",required = false) String name){
        return R.success(regionService.queryPageRegionList(pageNum,pageSize,name));
    }

    @GetMapping("list")
    public R<List<Region>> list(){
        return R.success(regionService.regionList());
    }
}
